<template>
  <div>
    <el-table v-loading="loading" :data="noticeList">
      <el-table-column label="序号" align="center" prop="order" width="50" />
      <el-table-column label="任务单编号" align="center" prop="noticeType" min-width="160">
        <template slot-scope="scope">
          <el-link @click="handledetailed(scope.row)" type="primary">{{ scope.row.upkeepTaskNum || '-' }}</el-link>
        </template>
      </el-table-column>
      <el-table-column label="申请单" align="center" min-width="160">
        <template slot-scope="scope">
          {{ scope.row.ukPlanPartInfoVo && scope.row.ukPlanPartInfoVo.upkeepPlanNum || '-' }}
        </template>
      </el-table-column>
      <el-table-column label="设备编号" align="center" min-width="130">
        <template slot-scope="scope">
          {{ scope.row.equipmentNum || '-' }}
        </template>
      </el-table-column>
      <el-table-column label="设备名称" align="center" min-width="130">
        <template slot-scope="scope">
          {{ scope.row.equipmentName || '-' }}
        </template>
      </el-table-column>
      <el-table-column label="保养人" align="center" min-width="150" show-overflow-tooltip>
        <template slot-scope="scope">
          <span v-for="(v, i) in scope.row.upkeepPlanMaintainerList" :key="i">
            {{ v.maintainerName || '-' }}
            <template v-if="i !== scope.row.upkeepPlanMaintainerList.length - 1">
              ,
            </template>
          </span>
          <!-- {{ scope.row.ukPlanPartInfoVo && scope.row.ukPlanPartInfoVo.headName || '-' }} -->
        </template>
      </el-table-column>
      <el-table-column label="保养部门" align="center" min-width="120">
        <template slot-scope="scope">
          {{ scope.row.ukPlanPartInfoVo && scope.row.ukPlanPartInfoVo.deptName || '-' }}
        </template>
      </el-table-column>
      <el-table-column label="保养类型" align="center" min-width="160">
        <template slot-scope="scope">
          {{ ['循环保养', '单次保养'][scope.row.ukPlanPartInfoVo && scope.row.ukPlanPartInfoVo.generateTaskType] || '-' }}
        </template>
      </el-table-column>
      <el-table-column label="验收人" align="center" min-width="120">
        <template slot-scope="scope">
          {{ scope.row.ukPlanPartInfoVo && scope.row.ukPlanPartInfoVo.acceptorName || '-' }}
        </template>
      </el-table-column>
      <el-table-column label="验收方式" align="center" min-width="120">
        <template slot-scope="scope">
          {{ scope.row.ukPlanPartInfoVo && scope.row.ukPlanPartInfoVo.acceptanceModeName || '-' }}
        </template>
      </el-table-column>
      <el-table-column label="保养状态" align="center" min-width="120">
        <template slot-scope="scope">
          <dict-tag :options="upkeepStatus" :value="scope.row.upkeepTaskStatuses[0].upkeepStatus" />
        </template>
      </el-table-column>
      <el-table-column label="验收状态" align="center" min-width="120">
        <template slot-scope="scope">
          <dict-tag :options="acceptanceStatus" :value="scope.row.upkeepTaskStatuses[0].acceptanceStatus" />
        </template>
      </el-table-column>
      <el-table-column label="计划保养日期" align="center" min-width="160">
        <template slot-scope="scope">
          {{ scope.row.planUpkeepTime || '-' }}
        </template>
      </el-table-column>
      <el-table-column label="创建部门" align="center" min-width="180" show-overflow-tooltip>
        <template slot-scope="scope">
          {{ scope.row.deptName || '-' }}
        </template>
      </el-table-column>
      <el-table-column label="创建人" align="center" min-width="120" show-overflow-tooltip>
        <template slot-scope="scope">
          {{ scope.row.applicantName || '-' }}
        </template>
      </el-table-column>
      <el-table-column label="操作" width="180" fixed="right" align="center" class-name="medium-padding fixed-width">
        <template slot-scope="scope">
          <!-- 负责人必须为登录人才能操作该数据 -->
          <template v-if="scope.row.ukPlanPartInfoVo && scope.row.ukPlanPartInfoVo.headName === username">
            <!-- 保养状态等于待执行以及重新保养 -->
            <el-button v-hasPermi="['insurance_Task_Finish']"
              v-if="scope.row.upkeepTaskStatuses[0].upkeepStatus === '1' || scope.row.upkeepTaskStatuses[0].upkeepStatus === '2'"
              size="small" type="primary" icon="el-icon-finished" @click="handleSubmit(scope.row)">保养完成
            </el-button>
            <!-- 保养状态未开始 -->
            <el-button v-hasPermi="['insurance_Task_start']" v-if="scope.row.upkeepTaskStatuses[0].upkeepStatus === '0'"
              size="small" type="success" icon="el-icon-refresh" @click="handlestart(scope.row)">开始保养
            </el-button>
          </template>
          <template v-if="scope.row.ukPlanPartInfoVo && scope.row.ukPlanPartInfoVo.acceptorName === username">
            <!-- 保养状态等于已完成并且验收状态为待验收 -->
            <el-button v-hasPermi="['insurance_Task_acceptance']"
              v-if="scope.row.upkeepTaskStatuses[0].upkeepStatus === '3' && scope.row.upkeepTaskStatuses[0].acceptanceStatus === '0'"
              size="small" type="primary" icon="el-icon-finished" @click="handleUpdateCommit(scope.row)">验收完成
            </el-button>
          </template>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />
    <!-- 完成保养 -->
    <template v-if="open">
      <MaintainanceFinish :rowObj="rowObj"></MaintainanceFinish>
    </template>
    <!-- 查看设备基值详情 -->
    <template v-if="opendetailed">
      <Detailed :rowObj="detailedObj"></Detailed>
    </template>
  </div>
</template>

<script>
import { getSelectMinaTaskList, putStartReplaceRecord } from "@/api/maintenance/insurance/insurance.js";

export default {
  // dicts: ['sys_notice_status', 'sys_notice_type'],
  components: {
    MaintainanceFinish: () => import('../form/maintainanceFinish.vue'),
    Detailed: () => import('../form/detailed.vue')
  },
  data () {
    return {
      // 遮罩层
      loading: true,
      opendetailed: false,
      detailedObj: {},
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      dictdatalist: [],
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 弹出层表格数据
      noticeList: [],
      rowObj: {},
      // 弹出层标题
      title: "修改类别",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderByColumn: 'upkeepTaskId',
        isAsc: 'desc'//倒叙
      },

    };
  },
  created () {
  },
  mounted () {
    if (!this.$route.query.isCondition) {
      this.getList();

    }
  },
  computed: {
    username () {
      return this.$store.state.user.name
    }
  },
  methods: {
    /** 查询列表 */
    async getList (value) {
      this.loading = true;
      let params = {
        ...this.queryParams,
        ...value
      }
      getSelectMinaTaskList(params).then(response => {
        this.noticeList = response.data && response.data.map((item, index) => {
          return {
            ...item,
            order: index + 1
          }
        });
        this.total = response.total;
        this.loading = false;
      });
    },
    // 打开详细弹窗
    handledetailed (row) {
      this.$parent.$parent.$parent.$parent.tabIndex = true
      this.$parent.$parent.$parent.$parent.activeName = '3'
      this.$parent.$parent.$parent.$parent.rowObj = row
    },
    /** 修改按钮操作 */
    handleUpdate (row) {
      this.rowObj = row
      this.open = true
    },
    // 多选框选中数据
    handleSelectionChange (selection) {
      this.ids = selection.map(item => item.noticeId)
      this.single = selection.length != 1
      this.multiple = !selection.length
    },
    /**保养完成 */
    handleSubmit (row) {
      this.rowObj = row
      this.open = true
    },
    /**验收完成 */
    handleUpdateCommit (row) {
      this.opendetailed = true
      this.detailedObj = row
    },
    /** 开始保养按钮操作 */
    handlestart (row) {
      const noticeIds = row.equipmentNum || '无名称'
      this.$confirm(`是否确认给设备名称为${noticeIds}的设备开始保养？`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.loading = true;
        let params = {
          upkeepTaskId: row.upkeepTaskId,
        }
        putStartReplaceRecord(params).then(res => {
          if (res.code === 200) {
            this.loading = false;
            this.$message.success(res.msg)
            this.getList()
          }
        })
      }).catch(() => { this.loading = false; });
    }
  },
  filters: {
  }
};
</script>
<style lang="scss" scoped>
.red {
  color: red;
}
</style>